package com.hod.reservgrid.client.model;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gears.client.database.DatabaseException;

public class ReservationRecordset {
	
	public static boolean setStateFinished(Integer idReservation){
		try {
			GearsDataStore ds = (GearsDataStore) GWT.create(Reservation.class);
			Reservation rs = (Reservation) ds.findById(idReservation);
			rs.setIdReservationState(2); // Estado de reserva finalizado
			//RoomTypeCRUD.increaseRoomTypeCapacity(rs.getIdRoomType()); //incremento la capacidad de reserva
			ds.save(rs);
			return true;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return false;
		}
	}
	public static boolean delete(Integer idReservation){
		try {
			GearsDataStore ds = (GearsDataStore) GWT.create(Reservation.class);
			ds.delete((Reservation) ds.findById(idReservation));
			return true;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			return false;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return false;
		}
	}
	
	public static ReservationState[] getReservationStates(){
		try {
			GearsDataStore dsReservationState = (GearsDataStore) GWT.create(ReservationState.class);
			GearsQuery q = dsReservationState.getNamedQuery(ReservationState.FIND_ALL);
			ReservationState[] data = (ReservationState[]) q.execute();
			//dsReservationState.close();
			return data;
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return null;
		} 
    } 
	
	public static double getBeck(Integer idReservation) {
		// TODO Auto-generated method stub
		GearsDataStore ds = (GearsDataStore) GWT.create(Reservation.class);
		Reservation r;
		try {
			r = (Reservation) ds.findById(idReservation);
		} catch (DatabaseException e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return r.getBeck();
	}
	public static Reservation[] getReservationByRoom(String idRoom){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Reservation.class);
 			GearsQuery q = dsRoom.getNamedQuery(Reservation.FIND_BY_ROOM);
 			q.setString(0, idRoom);
 			Reservation[] data = (Reservation[]) q.execute();
 			return data;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
		
	}
	public static boolean existsReservationByRoomInDate(String idRoom,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_ROOM_RESERVATION_IN_DATE);
 			q.setString(0, idRoom);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) return true;
 			return false;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return false;
 		} 
		
	}
	
	public static boolean existsReservationByBedInDate(String idBed,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_BED_RESERVATION_IN_DATE);
 			q.setString(0, idBed);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) return true;
 			return false;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return false;
 		} 
		
	}
	
	public static Client getClientReservationByRoomInDate(String idRoom,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_ROOM_RESERVATION_IN_DATE);
 			q.setString(0, idRoom);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) return data[0]; 
 			return data[0]; 			
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
		
	}
	
	public static Client getClientReservationByBedInDate(String idBed,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_BED_RESERVATION_IN_DATE);
 			q.setString(0, idBed);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) return data[0];
 			return null;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
		
	}
	
	public static Client getReservationByBedInDate(String idBed,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_BED_RESERVATION_IN_DATE);
 			q.setString(0, idBed);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) {
 			   return data[0];
 			}
 			return null;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
		
	}
	
	public static Client getReservationByRoomInDate(String idRoom,Date date){
		try {
 			GearsDataStore dsRoom = (GearsDataStore) GWT.create(Client.class);
 			GearsQuery q = dsRoom.getNamedQuery(Client.EXISTS_ROOM_RESERVATION_IN_DATE);
 			q.setString(0, idRoom);
 			q.setDate(1, date);
 			Client[] data = (Client[]) q.execute();
 			if (data.length>0) {
 				return data[0];
 			}
 			return null;
 		} catch (DatabaseException e) {
 			// TODO Auto-generated catch block
 			return null;
 		} 
		
	}

}
